import 'package:bruno/bruno.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_app/bean/list_integral_info_entity.dart';
import 'package:flutter_app/values/MyColors.dart';
import 'package:flutter_app/widget/custom_app_bar.dart';
import 'package:flutter_app/widget/empty_view.dart';
import 'package:flutter_screenutil/src/size_extension.dart';
import 'package:get/get.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';

import '../../../r.dart';
import 'accumulate_points_logic.dart';

/**
 * @Description: 积分明细
 * @Author: 下雨了
 * @CreateDate: 2022年5月31日 13:59:55
 * @Version: 1.0
 */
///
class AccumulatePointsPage extends StatelessWidget {
  final String title;

  const AccumulatePointsPage({
    Key? key,
    required this.title,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    final logic = Get.put(AccumulatePointsLogic());
    final state = Get.find<AccumulatePointsLogic>().state;

    return GetBuilder<AccumulatePointsLogic>(builder: (logic) {
      return Scaffold(
        backgroundColor: MyColors.FFF1F1F1,
        body: Stack(
          children: [
            Image.asset(R.assetsImgBg113, fit: BoxFit.fitWidth,),
            Column(
              children: [
                BrnAppBar(
                  backgroundColor: Colors.transparent,
                  title: Row(
                    mainAxisSize: MainAxisSize.min,
                    crossAxisAlignment: CrossAxisAlignment.center,
                    children: <Widget>[
                      DropdownButton(
                        items: [
                          for (String item in logic.yearItems) DropdownMenuItem(
                            child: Text(item + "年"), value: item,)
                        ],
                        // 下拉文本样式
                        style: TextStyle(color: MyColors.FFFFFFFF, fontSize: 16.sp),
                        dropdownColor: MyColors.D5F5F5F,
                        elevation: 1,// 阴影高度
                        value: logic.year,
                        hint: Text(logic.year, style: TextStyle(color: MyColors.FFFFFFFF, fontSize: 16.sp, fontWeight: FontWeight.w500),),
                        icon: Icon(Icons.arrow_drop_down_outlined, color: MyColors.FFFFFFFF,),
                        iconSize: 20.r,
                        underline: Container(height: 0,),    // 按钮下划线
                        isDense: true, // 是否降低按钮高度
                        onChanged: (value){
                          if (logic.year == value.toString()) {
                            return;
                          }
                          logic.year = value.toString();
                          logic.listIntegralInfo(state.currentPage);
                        },
                      ),
                    ],
                  ),
                  leading: DefaultBack(context),
                  leadingWidth: 55.r,
                  actions: [
                    TextButton(
                      child: Text("积分说明", style: TextStyle(color: MyColors.FFFFFFFF, fontSize: 14.sp),),
                      onPressed: () {
                        logic.goToWebViewPage();
                      },
                    ),
                  ],
                ),
                BrnNoticeBar(
                  content: '注：效能监督员总积分按自然年进行计算汇总',
                  showLeftIcon:false,
                  showRightIcon: false,
                  backgroundColor: Colors.transparent,
                  textColor: MyColors.FFFFFFFF,
                  noticeStyle: NoticeStyles.runningWithArrow,
                  onNoticeTap: () {
                    BrnToast.show('点击通知', context);
                  },
                ),
                Expanded(
                  child: Container(
                    margin: EdgeInsets.only(top: 12.r, right: 16.r, left: 16.r, bottom: 16.r),
                    decoration: BoxDecoration(
                      borderRadius: BorderRadius.circular(4.r),
                      color: MyColors.FFFFFFFF
                    ),
                    child: Column(
                      children: [
                        Container(
                          height: 24.r,
                          width: 82.r,
                          alignment: Alignment.center,
                          margin: EdgeInsets.only(top: 16.r, bottom: 10.r),
                          child: Stack(
                            children: [
                              Align(
                                alignment: Alignment.topLeft,
                                child: SizedBox(
                                  width: 16.5.r, height: 16.5.r,
                                  child: Container(
                                    decoration: BoxDecoration(
                                      gradient: LinearGradient(
                                        begin: Alignment.topCenter,
                                        end: Alignment.bottomCenter,
                                        colors: [
                                          MyColors.FFFDE2E1,
                                          MyColors.FFFFFDFD
                                        ],
                                      ),
                                      borderRadius: BorderRadius.only(topLeft: Radius.circular(8.25.r), bottomLeft: Radius.circular(8.25.r),)
                                    ),
                                  ),
                                ),
                              ),
                              Align(
                                alignment: Alignment.bottomRight,
                                child: SizedBox(
                                  width: 16.5.r, height: 16.5.r,
                                  child: Container(
                                    decoration: BoxDecoration(
                                      gradient: LinearGradient(
                                        begin: Alignment.topCenter,
                                        end: Alignment.bottomCenter,
                                        colors: [
                                          MyColors.FFFFFDFD,
                                          MyColors.FFFDE2E1
                                        ],
                                      ),
                                      borderRadius: BorderRadius.only(topRight: Radius.circular(8.25.r), bottomRight: Radius.circular(8.25.r),)
                                    ),
                                  ),
                                ),
                              ),
                              Center(child: Text("积分明细", style: TextStyle(color: MyColors.FFE0382F, fontSize: 16.sp, fontWeight: FontWeight.w600),)),
                            ],
                          ),
                        ),
                        Expanded(
                          child: SmartRefresher(
                            controller: logic.refreshController,
                            //  允许下拉刷新
                            enablePullDown: true,
                            // 允许上拉加载
                            enablePullUp: true,
                            // 刷新
                            onRefresh: logic.onRefresh,
                            // 加载
                            onLoading: logic.onLoading,
                            header: WaterDropHeader(complete: Text('刷新完成', style: TextStyle(color: MyColors.FFF03F35))),
                            footer: ClassicFooter(loadingText: '加载中...', idleText: '上拉加载更多..', noDataText: '没有更多数据了'),
                            child: (state.listIntegralInfoEntity?.content?.length ?? 0) > 0 ? ListView.separated(
                              physics: BouncingScrollPhysics(),
                              padding: EdgeInsets.only(top: 0),
                              itemCount: state.listIntegralInfoEntity?.content?.length ?? 0,
                              itemBuilder: (BuildContext context, int index) {
                                return ListItem(state.listIntegralInfoEntity!.content![index]);
                              },
                              separatorBuilder: (BuildContext context, int index) {
                                return Container(
                                  height: 0.5.r,
                                  color: MyColors.FFDDDDDD,
                                );
                              },
                            ) : EmptyView(),
                          ),
                        ),
                      ],
                    ),
                  ),
                ),
              ],
            ),
          ],
        ),
      );
    });
  }
  Widget ListItem(ListIntegralInfoContent item) {
    return Container(
      margin: EdgeInsets.only(left: 16.r, right: 16.r, top: 11.5.r, bottom: 11.r),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          Flexible(
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Text(
                  (item.title ?? "").isNotEmpty ? item.title ?? '' : item.remark ??'',
                  // "参加第三季度明查暗访",
                  maxLines: 2,
                  overflow: TextOverflow.ellipsis,
                  style: TextStyle(color: MyColors.FF333333, fontSize: 14.sp, fontWeight: FontWeight.w600),
                ),
                SizedBox(height: 2.5.r,),
                Text(item.createTime ?? "", style: TextStyle(color: MyColors.FF999999, fontSize: 11.sp),),
              ],
            ),
          ),
          Text(double.parse(item.score ?? '0') > 0 ? "+${item.score ?? ''}" : "${item.score ?? ''}",
            style: TextStyle(color: MyColors.FF333333, fontSize: 18.sp, fontWeight: FontWeight.w600),
          ),
        ],
      ),
    );
  }
}
